package org.jeecg.modules.monitoring.j_monitoring.service.impl;

import com.baomidou.mybatisplus.core.enums.SqlMethod;
import org.jeecg.modules.monitoring.j_monitoring.entity.MCabinet;
import org.jeecg.modules.monitoring.j_monitoring.entity.MGlDriveCabinet;
import org.jeecg.modules.monitoring.j_monitoring.entity.MGlPeripheral;
import org.jeecg.modules.monitoring.j_monitoring.entity.MMachineRoom;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MCabinetMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MGlPeripheralMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MMachineRoomMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.IMGlPeripheralService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 外设(管理)
 * @Author: xiaoyq
 * @Date:   2023-01-13
 * @Version: V1.0
 */
@Service
public class MGlPeripheralServiceImpl extends ServiceImpl<MGlPeripheralMapper, MGlPeripheral> implements IMGlPeripheralService {
    @Resource
    private MCabinetServiceImpl cabinetService;
    @Resource
    private MMachineRoomServiceImpl roomService;
    @Resource
    private MMachineRoomMapper mMachineRoomMapper;
    @Resource
    private MCabinetMapper cabinetMapper;

    public boolean saveBatch(Collection<MGlPeripheral> entityList, int size){

        //机柜列表
        List<MCabinet> cabinetList = cabinetMapper.selectAll();

        //机房列表
        List<MMachineRoom> roomList = mMachineRoomMapper.selectAll();

        //处理机房机柜id绑定问题
        for (MGlPeripheral entity : entityList){
            entity.setCabinetId(cabinetService.getId(entity.getCabinet(),cabinetList));
            entity.setMachineRoomId(roomService.getId(entity.getMachineRoom(),roomList));
        }


        String sqlStatement = this.getSqlStatement(SqlMethod.INSERT_ONE);
        return this.executeBatch(entityList, size, (sqlSession, entity) -> {
            sqlSession.insert(sqlStatement, entity);
        });

    }

}
